 let superagent  =require('superagent');
let  querystring = require('querystring'); 

//创建AIP FaceClient

let AipFaceClient = require("baidu-aip-sdk").face;

// 设置APPID/AK/SK
let APP_ID = "11418154";
let API_KEY = "AEN1bRrRKYalhSiCuVrG7str";
let SECRET_KEY = "mIYOxy83Sn00OhVtDrELNGvu9obmrGXM";

// 新建一个对象，建议只保存一个对象调用服务接口
let client = new AipFaceClient(APP_ID, API_KEY, SECRET_KEY);

//调用百度Ai人脸识别

  exports.faceRecognition  = async function (img) {
    //try catch 捕获异常
    try {
  let image = img;
  let imageType ='URL'
   // 调用人脸检测
   let results = await  client.detect(image, imageType)
   if(results.error_msg=='SUCCESS'){
    return true;
   }
   else{
    return false;
   }
    }
    catch(err){
        let errMsg='error';
        return errMsg ;
    }

    }


























// //狂视人脸识别api调用
// exports.faceRecognition  = async function (img) {

// //配置人脸识别api 秘钥
// let api_key ='eZf2mb2gcoIxxj7Imejze73nHzyf6_KB';
// let api_secret ='mM28ExVrnRgHb771P24lOoNuOh3ArDeW';
// //获取文件路径
// let filePath = path.join(__dirname + '/../../upload/'+img.slice(img.indexOf('avatar')));
// let data = fs.readFileSync(filePath); 
// //将图片转化为base64
// data = new Buffer(data).toString('base64');  
// let base64 = 'data:' + mineType.lookup(filePath) + ';base64,' + data; 
// let  reqUrl  = 'https://api-cn.faceplusplus.com/facepp/v3/detect';
// let  reqData = querystring.stringify({
//     api_key:"eZf2mb2gcoIxxj7Imejze73nHzyf6_KB",
//     api_secret:"mM28ExVrnRgHb771P24lOoNuOh3ArDeW",
//     image_url:"http://p3d278els.bkt.clouddn.com/faces.jpg"//图片地址，接口支持base64,图片地址和imagefile
// })


//   let result = await superagent.post(reqUrl).set('Content-Type','application/x-www-form-urlencoded; charset=UTF-8').send(reqData);
//   console.log(result.body)
// }
//  exports.faceRecognition  = async function (img) {
// let data = querystring.stringify({
//     api_key:"eZf2mb2gcoIxxj7Imejze73nHzyf6_KB",
//     api_secret:"mM28ExVrnRgHb771P24lOoNuOh3ArDeW",
//     image_url:img//图片地址，接口支持base64,图片地址和imagefile
// })
// let result =  await superagent.post('https://api-cn.faceplusplus.com/facepp/v3/detect')
//     .set('Content-Type','application/x-www-form-urlencoded; charset=UTF-8').send(data);
//     let faceRestults = result.body.faces;  
//     if(faceRestults.length>0){
//       return true;
//       }
//       return false;
//       }
